﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.IO;
using ECCIT;
using ECCIT.Extension.Weibo_v2.Model;
using ECCIT.Extension.Weibo_v2.Common;
using OgilvyOne.BLL;
using OgilvyOne.Model;
using OgilvyOne.UKNow.BLL;
using OgilvyOne.UKNow.Configuration;
using OgilvyOne.UKNow.Mobile.Model;
using OgilvyOne.UKNow.Mobile.Model.MyCalendar;
using OgilvyOne.UKNow.Mobile.Utilities;
using OgilvyOne.UKNow.Model;
using OgilvyOne.UKNow.Utility;
using OgilvyOne.Utility;

namespace OgilvyOne.UKNow.Mobile.APIs.HotSpot
{
  public  class GetList:IHttpHandler
    {
        public bool IsReusable
        {
            get { throw new NotImplementedException(); }
        }

        public void ProcessRequest(HttpContext context)
        {
            string pagesize = context.Request.QueryString["page_size"];
            string pageindex = context.Request.QueryString["current_page"];
            int ipagesize = 10;
            int ipageindex = 1;
            if(pagesize!=null)
            {
                if(int.TryParse(pagesize,out ipagesize))
                {
                }
            }
            if(pageindex!=null)
            {
                if(int.TryParse(pageindex,out ipageindex))
                {
                }
            }

            try
            {
              
                var eventsBll=new EventsBLL(2052);
                PageTools pagemodel = eventsBll.GetHotspotList(ipageindex, ipagesize);
               // if(pagemodel.Counts>0)
                {
                    List<HotSpotInfo> newslist = new List<HotSpotInfo>();
                    CmsArticleImgsBll bll=new CmsArticleImgsBll();
                   
                    var arrayList = (ArrayList)pagemodel.DataList;
                    foreach (CmsArticleModel model in arrayList)
                    {
                        //if (DateTimeFormatter.FormatDate(model.Starttime) != DateTimeFormatter.FormatDate(DateTime.Now))
                        //    continue;

                        var hotSpotInfo = new HotSpotInfo();
                        hotSpotInfo.Content = model.Summary;
                        var imgmodel = new CmsArticleImgModel();
                        imgmodel.ArticleId = (int) model.ID;
                        var models = bll.GetImgsList(imgmodel).Cast<CmsArticleImgModel>().ToList();
                        var images = models.Where(p => p.ImgType == 5).ToList();
                        hotSpotInfo.Images = (from mm in images
                                                select new HotPhoneImage
                                                            {
                                                                Imagpath =!string.IsNullOrEmpty(mm.ImgFileName)? ConfigCacheManager.DomainName + ConfigCacheManager.FilePath.GetCmsImageVirtualPath(mm.ImgFileName):string.Empty
                                                            }).ToList();
                        
                        hotSpotInfo.ID = model.ID;
                        hotSpotInfo.InsertedTime = DateTimeFormatter.FormatDateTime(model.Starttime);
                        newslist.Add(hotSpotInfo);
                    }

                    var hotlist = new PageOfItems<HotSpotInfo>();
                    hotlist.PageIndex = ipageindex;
                    hotlist.PageSize = ipagesize;
                    hotlist.Data = newslist;
                    hotlist.TotalItemCount = pagemodel.Counts;
                    hotlist.TotalPageCount = pagemodel.PageCount();
                    string s = JsonSerializationHelper.ObjectToJson(hotlist);
                    context.Response.Write(ResultUtility.ReturnResultJson("0", s, "获取每日新鲜事列表成功"));

                }
            }
            catch (Exception ex)
            {
                context.Response.Write(ResultUtility.ReturnResultJson("-101", "", "获取每日新鲜事列表失败. Message:" + ex.Message));
                //throw;
            }



        }
    }
}
